<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <style>

        .leftfix {
            float: left;
        }

        .rightfix {
            float: right;
        }

        .clearfix::after {
            content: "";
            display: block;
            clear: both;
        }
        .content {
            width: 700px;
            height: 60px;
            margin: 20px auto;
            display: flex;
            justify-content: space-between;
            transition: all 500ms;
            margin-bottom: 0;
        }

        .content textarea {
            resize: none;
            width: 600px;
            margin-right: 10px;
            padding: 10px;
            /* height: 50px; */
        }

        .publish {
            width: 80px;
            background-color: dodgerblue;
            border: none;
            border-radius: 10px;
            color: white;
            cursor: pointer;
        }

        .focus {
            height: 100px;
            
        }

        .hint {
            width: 300px;
            text-align: right;
            margin: 0 auto;
            color: gray;
            font-size: 14px;
            display: none;
        }

        .page-comment {
            margin: 0 auto;
            width: 700px;
        }

        .page-comment .item {
            height: 80px;
            margin-top: 10px;
        }

        .page-comment .item img {
            height: 50px;
            border-radius: 50%;
            display: inline;
        }

        .page-comment .touxiang {
            width: 70px;
            text-align: center;
        }

        .page-comment .item .neirong {
            height: 80px;
            width: 600px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            border-bottom: 1px dashed gray;
            /* background-color: aqua; */
        }

        .user-name {
            font-size: 14px;
            color: rgb(252, 75, 136);
        }

        .publish-time {
            color: gray;
            font-size: 13px;
        }
    </style>
</head>
<body>

    <div class="page-comment">
        <div class="item">
            <div class="touxiang leftfix">
                <img src="img/ico.png" alt="头像">
            </div>
            <div class="neirong leftfix">
                <div class="user-name">
                    清风徐来
                </div>

                <div class="comment-content">
                    开心
                </div>

                <div class="publish-time">
                    2021.12.12
                </div>
            </div>
        </div>
        <div class="item">
            <div class="touxiang leftfix">
                <img src="img/ico.png" alt="头像">
            </div>
            <div class="neirong leftfix">
                <div class="user-name">
                    清风徐来
                </div>

                <div class="comment-content">
                    开心
                </div>

                <div class="publish-time">
                    2021.12.12
                </div>
            </div>
        </div>
        <div class="item">
            <div class="touxiang leftfix">
                <img src="img/ico.png" alt="头像">
            </div>
            <div class="neirong leftfix">
                <div class="user-name">
                    清风徐来
                </div>

                <div class="comment-content">
                    开心
                </div>

                <div class="publish-time">
                    2021.12.12
                </div>
            </div>
        </div>
    </div>

    <div class="content">
        <textarea name="comment" id="comment" placeholder="发一条友善的评论" maxlength="200"></textarea>
        <button class="publish">发布</button>
    </div>
    
    <div class="hint">
    </div>

<script>
    let text = document.querySelector(".content textarea")
    let content = document.querySelector(".content")
    let hint = document.querySelector(".hint")
    let send = document.querySelector("button")
    let comment = document.querySelector(".page-comment")
    text.addEventListener("focus", function() {
        content.classList.add("focus")
        hint.innerHTML = `${text.value.length}/200字`
        hint.style.display = "block"
    })

    text.addEventListener("input", function() {
        hint.innerHTML = `${text.value.length}/200字`
    })

    text.addEventListener("blur", function() {
        content.classList.remove("focus")
        hint.style.display = "none"
    })

    function pub() {
        if (text.value.length > 0) {
            comment.innerHTML += `
            
            <div class="item">
            <div class="touxiang leftfix">
                <img src="img/ico.png" alt="头像">
            </div>
            <div class="neirong leftfix">
                <div class="user-name">
                    清风徐来
                </div>

                <div class="comment-content">
                    ${text.value}
                </div>

                <div class="publish-time">
                    ${new Date().toLocaleString()}
                </div>
            </div>
        </div>
            `
            text.value = ''
        }
    }

    send.addEventListener("click", pub)

    text.addEventListener("keypress", function(e) {
        if (e.ctrlKey && e.key == "Enter") {
            pub()
        }
    })
</script>
</body>
</html>